﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace d1
{

    // 订单明细类
    class OrderDetails
    {
        public string ProductName { get; set; }
        public double Price { get; set; }
        public int Quantity { get; set; }

        public override bool Equals(object obj)
        {
            if (obj is OrderDetails other)
            {
                return ProductName == other.ProductName && Price == other.Price && Quantity == other.Quantity;
            }
            return false;
        }

        public override string ToString()
        {
            return $"Product: {ProductName}, Price: {Price}, Quantity: {Quantity}";
        }
    }

    // 订单类
    class Order
    {
        public int OrderId { get; set; }
        public string Customer { get; set; }
        public List<OrderDetails> Details { get; set; } = new List<OrderDetails>();

        public double TotalAmount => Details.Sum(d => d.Price * d.Quantity);

        public override bool Equals(object obj)
        {
            if (obj is Order other)
            {
                return OrderId == other.OrderId && Customer == other.Customer && Details.SequenceEqual(other.Details);
            }
            return false;
        }

        public override string ToString()
        {
            return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {TotalAmount}";
        }
    }

    // 订单服务类
    class OrderService
    {
        private List<Order> orders = new List<Order>();

        // 添加订单
        public void AddOrder(Order order)
        {
            if (orders.Contains(order))
            {
                throw new ArgumentException("Order already exists.");
            }
            orders.Add(order);
        }

        // 删除订单
        public void RemoveOrder(int orderId)
        {
            Order order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order != null)
            {
                orders.Remove(order);
            }
            else
            {
                throw new ArgumentException("Order not found.");
            }
        }

        // 修改订单
        public void ModifyOrder(Order newOrder)
        {
            Order existingOrder = orders.FirstOrDefault(o => o.OrderId == newOrder.OrderId);
            if (existingOrder != null)
            {
                existingOrder.Customer = newOrder.Customer;
                existingOrder.Details = newOrder.Details;
            }
            else
            {
                throw new ArgumentException("Order not found.");
            }
        }

        // 按订单号查询订单
        public Order QueryOrderByOrderId(int orderId)
        {
            return orders.FirstOrDefault(o => o.OrderId == orderId);
        }

        // 按商品名称查询订单
        public List<Order> QueryOrdersByProductName(string productName)
        {
            return orders.Where(o => o.Details.Any(d => d.ProductName == productName)).OrderByDescending(o => o.TotalAmount).ToList();
        }

        // 按客户查询订单
        public List<Order> QueryOrdersByCustomer(string customer)
        {
            return orders.Where(o => o.Customer == customer).OrderByDescending(o => o.TotalAmount).ToList();
        }

        // 按订单金额查询订单
        public List<Order> QueryOrdersByAmount(double amount)
        {
            return orders.Where(o => o.TotalAmount == amount).OrderByDescending(o => o.TotalAmount).ToList();
        }

        // 对保存的订单进行排序，默认按照订单号排序
        public void SortOrders()
        {
            orders = orders.OrderBy(o => o.OrderId).ToList();
        }

        // 自定义排序
        public void SortOrders(Func<Order, object> keySelector)
        {
            orders = orders.OrderBy(keySelector).ToList();
        }
    }

    class Program
    {
        static OrderService orderService = new OrderService();

        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Order Management System!");

            while (true)
            {
                Console.WriteLine("\nAvailable commands:");
                Console.WriteLine("1. Add Order");
                Console.WriteLine("2. Remove Order");
                Console.WriteLine("3. Modify Order");
                Console.WriteLine("4. Query Order");
                Console.WriteLine("5. Exit");
                Console.Write("Please enter your choice: ");

                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        AddOrder();
                        break;
                    case "2":
                        RemoveOrder();
                        break;
                    case "3":
                        ModifyOrder();
                        break;
                    case "4":
                        QueryOrder();
                        break;
                    case "5":
                        Console.WriteLine("Exiting program...");
                        return;
                    default:
                        Console.WriteLine("Invalid choice. Please try again.");
                        break;
                }
            }
        }

        static void AddOrder()
        {
            Console.WriteLine("Adding new order...");

            Order newOrder = new Order();
            Console.Write("Enter Order ID: ");
            newOrder.OrderId = int.Parse(Console.ReadLine());

            Console.Write("Enter Customer Name: ");
            newOrder.Customer = Console.ReadLine();

            Console.WriteLine("Enter Order Details (Press Enter to stop adding details):");
            while (true)
            {
                OrderDetails details = new OrderDetails();

                Console.Write("Enter Product Name: ");
                details.ProductName = Console.ReadLine();

                Console.Write("Enter Price: ");
                details.Price = double.Parse(Console.ReadLine());

                Console.Write("Enter Quantity: ");
                details.Quantity = int.Parse(Console.ReadLine());

                newOrder.Details.Add(details);

                Console.Write("Add another item? (Y/N): ");
                string input = Console.ReadLine();
                if (input.ToLower() != "y")
                    break;
            }

            try
            {
                orderService.AddOrder(newOrder);
                Console.WriteLine("Order added successfully.");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


        static void RemoveOrder()
        {
            Console.WriteLine("Removing order...");
            Console.Write("Enter the order ID to remove: ");
            if (int.TryParse(Console.ReadLine(), out int orderId))
            {
                try
                {
                    orderService.RemoveOrder(orderId);
                    Console.WriteLine("Order removed successfully.");
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("Invalid order ID format.");
            }
        }

        static void ModifyOrder()
        {
            Console.WriteLine("Modifying order...");
            Console.Write("Enter the order ID to modify: ");
            if (int.TryParse(Console.ReadLine(), out int orderId))
            {
                Order existingOrder = orderService.QueryOrderByOrderId(orderId);
                if (existingOrder != null)
                {
                    // Display existing order details
                    Console.WriteLine("Existing Order Details:");
                    Console.WriteLine(existingOrder);

                    // Modify order details
                    Console.WriteLine("Enter new details:");

                    Console.Write("Enter Customer Name: ");
                    existingOrder.Customer = Console.ReadLine();

                    Console.WriteLine("Enter Order Details (Press Enter to stop adding details):");
                    existingOrder.Details.Clear();
                    while (true)
                    {
                        OrderDetails details = new OrderDetails();

                        Console.Write("Enter Product Name: ");
                        details.ProductName = Console.ReadLine();

                        Console.Write("Enter Price: ");
                        details.Price = double.Parse(Console.ReadLine());

                        Console.Write("Enter Quantity: ");
                        details.Quantity = int.Parse(Console.ReadLine());

                        existingOrder.Details.Add(details);

                        Console.Write("Add another item? (Y/N): ");
                        string input = Console.ReadLine();
                        if (input.ToLower() != "y")
                            break;
                    }

                    try
                    {
                        orderService.ModifyOrder(existingOrder);
                        Console.WriteLine("Order modified successfully.");
                    }
                    catch (ArgumentException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    Console.WriteLine("Order not found.");
                }
            }
            else
            {
                Console.WriteLine("Invalid order ID format.");
            }
        }


static void QueryOrder()
{
    Console.WriteLine("Querying order...");
    Console.WriteLine("Choose query type:");
    Console.WriteLine("1. By Order ID");
    Console.WriteLine("2. By Product Name");
    Console.WriteLine("3. By Customer");
    Console.WriteLine("4. By Total Amount");
    Console.Write("Enter your choice: ");
    string queryType = Console.ReadLine();

    switch (queryType)
    {
        case "1":
            Console.Write("Enter Order ID: ");
            if (int.TryParse(Console.ReadLine(), out int orderId))
            {
                Order order = orderService.QueryOrderByOrderId(orderId);
                if (order != null)
                {
                    Console.WriteLine("Order found:");
                    Console.WriteLine(order);
                }
                else
                {
                    Console.WriteLine("Order not found.");
                }
            }
            else
            {
                Console.WriteLine("Invalid order ID format.");
            }
            break;
        case "2":
            Console.Write("Enter Product Name: ");
            string productName = Console.ReadLine();
            List<Order> ordersByProduct = orderService.QueryOrdersByProductName(productName);
            if (ordersByProduct.Any())
            {
                Console.WriteLine($"Orders found for product '{productName}':");
                foreach (var order in ordersByProduct)
                {
                    Console.WriteLine(order);
                }
            }
            else
            {
                Console.WriteLine($"No orders found for product '{productName}'.");
            }
            break;
        case "3":
            Console.Write("Enter Customer Name: ");
            string customerName = Console.ReadLine();
            List<Order> ordersByCustomer = orderService.QueryOrdersByCustomer(customerName);
            if (ordersByCustomer.Any())
            {
                Console.WriteLine($"Orders found for customer '{customerName}':");
                foreach (var order in ordersByCustomer)
                {
                    Console.WriteLine(order);
                }
            }
            else
            {
                Console.WriteLine($"No orders found for customer '{customerName}'.");
            }
            break;
        case "4":
            Console.Write("Enter Total Amount: ");
            if (double.TryParse(Console.ReadLine(), out double totalAmount))
            {
                List<Order> ordersByAmount = orderService.QueryOrdersByAmount(totalAmount);
                if (ordersByAmount.Any())
                {
                    Console.WriteLine($"Orders found with total amount '{totalAmount}':");
                    foreach (var order in ordersByAmount)
                    {
                        Console.WriteLine(order);
                    }
                }
                else
                {
                    Console.WriteLine($"No orders found with total amount '{totalAmount}'.");
                }
            }
            else
            {
                Console.WriteLine("Invalid total amount format.");
            }
            break;
        default:
            Console.WriteLine("Invalid choice.");
            break;
    }
}

    }

}
